using System;
using System.Configuration;
using System.Runtime.Remoting;
using System.Threading;
using ODX.Core;
using ODX.Remoting.Shared;

namespace ODX.Remoting.Client
{
    class Program
    {
        static void Main()
        {
            Thread.Sleep(1000);

            string fn = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
            RemotingConfiguration.Configure(fn, false);

            IServer server = (IServer)Activator.GetObject(typeof(IServer), "ipc://odx/Server");

            Session session = new Session((IDataProvider)Activator.GetObject(typeof(IServer), "ipc://odx/XmlData"));
            session.RegisterAssembly(typeof(Human).Assembly);
            session.Prepare();

            RemotingSessionManager.RegisterChannelSession(session);

            Console.Write("Initializing server...");
            Console.WriteLine("done!");

            Human john = session.Create<Human>();

            john.Name = "John";
            john.Age = 23;

            Console.Write("Handling integer...");
            server.HandleInt(123);
            Console.WriteLine("done!");

            Console.Write("Saving data...");
            session.Save();
            Console.WriteLine("done");
            Console.WriteLine("Data has been successfully sent to server.");

            Console.Write("Handling john...");
            Entity outRes;
            Entity res = server.HandleEntity(john, out outRes);
            Console.WriteLine("done!");
            Console.WriteLine(ReferenceEquals(res, john));
            Console.WriteLine(ReferenceEquals(outRes, john));
            Console.WriteLine(john.Age);

            Console.WriteLine("HasChanges: {0}", session.HasChanges());
        }
    }
}
